<?php
/**
 * Created by PhpStorm.
 * User: chang
 * Date: 2019/4/1
 * Time: 11:54
 */

namespace App\Http\Model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class UserRegisterLog extends Model
{
    protected $table = 'user_register_log';
    public $timestamps = false;
    
    /**
     * 创建日志
     * @param int $user_id
     * @param string $version_no
     * @param string $channel_id
     * @param string $device
     * @param string $os_name
     * @return bool
     */
    public function createdRegisterLog(int $user_id, string $version_no, string $channel_id, string $device, string $os_name, string $platform , $guid ,$callback='', $cid = '', $aid = '', $advertiserId = ''){
        $model = new UserRegisterLog;
        $model->user_id = $user_id;
        $model->version_no = $version_no;
        $model->channel_id = $channel_id;
        $model->device = $device;
        $model->os_name = trim($os_name);
        $model->time = time();
        $model->platform = $platform;
        $model->guid = $guid;
        $model->callback = $callback;
        $model->cid = $cid;
        $model->aid = $aid;
        $model->advertiser_id = $advertiserId;
        $model->ua = $_SERVER['HTTP_WEBVIEW_UA'] ?? '';//ua
        $model->client_ip = client_ip();
        $res = $model->save();
        return $res;
    }

    /**获取注册日志
     * @param $user_id
     * @return Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function getUserRegisterLog($user_id){
        return  DB::table($this->table)
            ->where('user_id', '=', $user_id)
            ->first();
    }

    /**更新有邀请码的注册用户的platform
     * @param $user_id
     * @param $platform
     */
    public function updatePlatform($user_id ,$platform){
        DB::table($this->table)
            ->where('user_id', $user_id)
            ->update(array('platform'=>$platform));
    }

    /**
     * Created at 2021/12/27 11:44
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param string $guid
     * @param array|string[] $select
     * @return array|mixed
     */
    public function findRecordByGuid(string $guid, array $select = ['*'])
    {
        if (!$guid) {
            return [];
        }

        $query = self::query()->select($select)->where('guid', $guid)->orderByDesc('id')->limit(1)->get()->toArray();
        return $query ? $query[0] : [];
    }
}